package main;

import model.Tarefa;
import model.Tarefas;

import org.json.JSONArray;
import org.json.JSONObject;

import utils.JsonUtils;

import adapter.TarefasAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.tarefas.R;

public class MainActivity extends Activity  {
	
	static final String URL_STR_LISTAR = "http://xserve2.telemidia.puc-rio.br/~moreno/afazeres/lista_afazeres.php";
	static final String MESSAGE= "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		try {
			JSONArray jsonArray = JsonUtils.getArray(URL_STR_LISTAR);
			Tarefas tarefas = new Tarefas();
			tarefas.parse(jsonArray);
			
			/*
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject o = jsonArray.getJSONObject(i);
				Toast.makeText(getApplicationContext(), o.toString(), Toast.LENGTH_LONG).show();
			}
			ListView listView = (ListView) findViewById(R.id.mylist);
			ArrayAdapter<Tarefa> arrayAdapter = new ArrayAdapter<Tarefa>(this, R.id.mylist);
			for (Tarefa tarefa : tarefas) {
				arrayAdapter.add(tarefa);
			}
			listView.setAdapter(arrayAdapter);
			*/
			
			
			ListView listView = (ListView)findViewById(R.id.afazeresList);
			listView.setAdapter(new TarefasAdapter(this,tarefas));
			
			listView.setClickable(true);
			listView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Toast.makeText(null, "FOI", Toast.LENGTH_LONG).show();
				}
			});
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
